package com.hebeu.device.utils;

import org.apache.log4j.Logger;

import java.util.Random;

/**
 * @author 言立慧
 * @date 2018/10/19 11:07
 */

public class StringUtils {

    private static Logger logger = Logger.getLogger(StringUtils.class.getName());

    /**
     * 生成长度字符串
     *
     * @param length
     * @return
     */
    public static String getRandomStringByLength(int length) {
        // 基于一下字符生成串
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 判断字符串是否为空
     *
     * @param ip
     * @return
     */
    public static boolean isNotEmpty(String ip) {
        return ip != null && !"".equals(ip);
    }

    /**
     * 判断字符串是否为空
     *
     * @param str 被判断的字符串
     * @param msg 输出信息
     * @return
     */
    public static boolean isNotEmpty(String str, String msg) {
        boolean isNull = isNotEmpty(str);
        if (!isNull) {
            logger.error(msg);
        }
        return isNull;
    }

    /**
     * 判断字符串为空
     *
     * @param xFor
     * @return
     */
    public static boolean isBlank(String xFor) {
        return xFor == null || "".equals(xFor);
    }
}
